import 'package:get/get.dart';
import 'package:honeypotflutter/modules/deliver/api/deliver_api.dart';
import 'package:honeypotflutter/public/public_alert.dart';
import 'package:honeypotflutter/public/public_total.dart';
import '../../model/deliver_result.dart';
import '../deliver_select/view.dart';

class DeliverFailLogic extends GetxController {
  DeliverResult? result;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    result = Get.arguments;
  }

  /*
  * 再次开门
  * */
  againOpenDoor() async {
    if (result == null || result!.id == null) {
      return;
    }
    HLAlert.showSureCancelAlert(
        title: "柜门未开",
        message: "是否重新确认代开柜门",
        sureAction: (bool isOK) async {
          DeliverResult deliverResult = await DeliverApi.againOpenDoor(result!.id!);
          if (deliverResult.isSucceed == false) {
            HLTotal.total(deliverResult.reason ?? "开门失败");
            return;
          }
          HLTotal.total("开门成功");
        });
  }

  /*
  * 确认
  * */
  sureAction() async {
    if (result == null || result!.id == null) {
      return;
    }
    await DeliverApi.sureAction(id: result!.id!, status: 1);
    Get.off(DeliverSelectPage(), arguments: true);
  }

  /*
  * 取消投递
  * */
  cancelDeliver(bool open) async {
    if (result == null || result!.id == null) {
      return;
    }
    Map<String, dynamic> param = {"DoorOpened": open};
    await DeliverApi.sureAction(id: result!.id!, status: 2, customParam: param);
    Get.off(DeliverSelectPage(), arguments: true);
  }
}
